using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Globalization;
using System.Windows.Forms;

namespace FB.WinForms.Controls
{
	/// <summary>
	/// The ShadowBrushDialog form is used by the <see cref="HtmlDropDownListItemsEditorUI"/>
	/// to enter the Brush tag with a type attribute of Shadow.
	/// </summary>
	public class ShadowBrushDialog : System.Windows.Forms.Form
	{
		private System.Windows.Forms.Button buttonOK;
		private System.Windows.Forms.Button buttonCancel;
		private System.Windows.Forms.PictureBox pictureBoxSample;
		private System.Windows.Forms.Label labelAngle;
		private System.Windows.Forms.Label labelSampleHeading;
		private System.Windows.Forms.Button buttonShadowColor;
		private System.Windows.Forms.NumericUpDown numericUpDownOffset;
		private System.ComponentModel.Container components = null;

		private Color mShadowColor = Color.Blue;
		private string mStartTag = string.Empty;

		/// <summary>
		/// Initialise a new instance of the <see cref="ShadowBrushDialog"/> class.
		/// </summary>
		/// <param name="color">A Color structure that represents the shadow color.</param>
		/// <param name="offset">The offset, measured in pixels, of the shadow.</param>
		public ShadowBrushDialog(Color color, int offset)
		{
			InitializeComponent();

			ShadowColor = color;
			Offset = offset;

			DrawSample(ShadowColor, Offset);
		}

		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if(components != null)
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#region Windows Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
			this.buttonOK = new System.Windows.Forms.Button();
			this.buttonCancel = new System.Windows.Forms.Button();
			this.pictureBoxSample = new System.Windows.Forms.PictureBox();
			this.labelAngle = new System.Windows.Forms.Label();
			this.numericUpDownOffset = new System.Windows.Forms.NumericUpDown();
			this.labelSampleHeading = new System.Windows.Forms.Label();
			this.buttonShadowColor = new System.Windows.Forms.Button();
			((System.ComponentModel.ISupportInitialize)(this.numericUpDownOffset)).BeginInit();
			this.SuspendLayout();
			// 
			// buttonOK
			// 
			this.buttonOK.DialogResult = System.Windows.Forms.DialogResult.OK;
			this.buttonOK.Location = new System.Drawing.Point(24, 168);
			this.buttonOK.Name = "buttonOK";
			this.buttonOK.TabIndex = 18;
			this.buttonOK.Text = "OK";
			// 
			// buttonCancel
			// 
			this.buttonCancel.DialogResult = System.Windows.Forms.DialogResult.Cancel;
			this.buttonCancel.Location = new System.Drawing.Point(104, 168);
			this.buttonCancel.Name = "buttonCancel";
			this.buttonCancel.TabIndex = 17;
			this.buttonCancel.Text = "Cancel";
			// 
			// pictureBoxSample
			// 
			this.pictureBoxSample.Location = new System.Drawing.Point(8, 104);
			this.pictureBoxSample.Name = "pictureBoxSample";
			this.pictureBoxSample.Size = new System.Drawing.Size(192, 50);
			this.pictureBoxSample.TabIndex = 16;
			this.pictureBoxSample.TabStop = false;
			// 
			// labelAngle
			// 
			this.labelAngle.Location = new System.Drawing.Point(48, 48);
			this.labelAngle.Name = "labelAngle";
			this.labelAngle.Size = new System.Drawing.Size(40, 23);
			this.labelAngle.TabIndex = 15;
			this.labelAngle.Text = "Offset:";
			// 
			// numericUpDownOffset
			// 
			this.numericUpDownOffset.Location = new System.Drawing.Point(104, 48);
			this.numericUpDownOffset.Maximum = new System.Decimal(new int[] {
																				10,
																				0,
																				0,
																				0});
			this.numericUpDownOffset.Minimum = new System.Decimal(new int[] {
																				1,
																				0,
																				0,
																				0});
			this.numericUpDownOffset.Name = "numericUpDownOffset";
			this.numericUpDownOffset.Size = new System.Drawing.Size(80, 20);
			this.numericUpDownOffset.TabIndex = 14;
			this.numericUpDownOffset.Value = new System.Decimal(new int[] {
																			  3,
																			  0,
																			  0,
																			  0});
			this.numericUpDownOffset.ValueChanged += new System.EventHandler(this.numericUpDownOffset_ValueChanged);
			// 
			// labelSampleHeading
			// 
			this.labelSampleHeading.Location = new System.Drawing.Point(0, 80);
			this.labelSampleHeading.Name = "labelSampleHeading";
			this.labelSampleHeading.Size = new System.Drawing.Size(48, 16);
			this.labelSampleHeading.TabIndex = 13;
			this.labelSampleHeading.Text = "Sample:";
			// 
			// buttonShadowColor
			// 
			this.buttonShadowColor.Location = new System.Drawing.Point(104, 8);
			this.buttonShadowColor.Name = "buttonShadowColor";
			this.buttonShadowColor.Size = new System.Drawing.Size(88, 23);
			this.buttonShadowColor.TabIndex = 11;
			this.buttonShadowColor.Text = "Shadow Color";
			this.buttonShadowColor.Click += new System.EventHandler(this.buttonShadowColor_Click);
			// 
			// ShadowBrushDialog
			// 
			this.AcceptButton = this.buttonOK;
			this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
			this.CancelButton = this.buttonCancel;
			this.ClientSize = new System.Drawing.Size(210, 200);
			this.ControlBox = false;
			this.Controls.Add(this.buttonOK);
			this.Controls.Add(this.buttonCancel);
			this.Controls.Add(this.pictureBoxSample);
			this.Controls.Add(this.labelAngle);
			this.Controls.Add(this.numericUpDownOffset);
			this.Controls.Add(this.labelSampleHeading);
			this.Controls.Add(this.buttonShadowColor);
			this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog;
			this.MaximizeBox = false;
			this.MinimizeBox = false;
			this.Name = "ShadowBrushDialog";
			this.ShowInTaskbar = false;
			this.SizeGripStyle = System.Windows.Forms.SizeGripStyle.Hide;
			this.Text = "Shadow Brush Dialog";
			((System.ComponentModel.ISupportInitialize)(this.numericUpDownOffset)).EndInit();
			this.ResumeLayout(false);

		}
		#endregion

		/// <summary>
		/// Gets the end tag of the Brush element.
		/// </summary>
		public string EndTag
		{
			get { return "</brush>"; }
		}
			
		/// <summary>
		/// Gets or sets the value of the shadow offset.
		/// </summary>
		public int Offset
		{
			get { return (int)numericUpDownOffset.Value; }
			set 
			{ 
				numericUpDownOffset.Value = value; 
				mStartTag = string.Empty;
			}
		}

		/// <summary>
		/// Gets or sets a Color structure that represents the shadow color.
		/// </summary>
		public Color ShadowColor
		{
			get { return mShadowColor; }
			set 
			{ 
				mShadowColor = value; 
				mStartTag = string.Empty;
			}
		}

		/// <summary>
		/// The hexadecimal triplet that represents the shadow color.
		/// </summary>
		public string ShadowColorRGB
		{
			get { return String.Format(CultureInfo.InvariantCulture, "{0:X2}{1:X2}{2:X2}", mShadowColor.R, mShadowColor.G, mShadowColor.B); }
		}

		/// <summary>
		/// Gets the start tag of the Brush element type Shadow.
		/// </summary>
		public string StartTag
		{
			get 
			{
				if (mStartTag.Length == 0)
				{
					mStartTag = String.Format(CultureInfo.InvariantCulture, "<brush type=\"Shadow\" ShadowColor=\"#{0}\" Offset=\"{1}\">", ShadowColorRGB, Offset);
				}
				return mStartTag; 
			}
		}
			
		/// <summary>
		/// Handles the Shadow Color button click event.
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void buttonShadowColor_Click(object sender, System.EventArgs e)
		{
			ColorDialog colorDialog = new ColorDialog();
			if (colorDialog.ShowDialog() == DialogResult.OK)
			{
				ShadowColor =  colorDialog.Color;
				DrawSample(ShadowColor, Offset);
			}		
		}

		/// <summary>
		/// Handles the Offset NumericUpDown control value change event.
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void numericUpDownOffset_ValueChanged(object sender, System.EventArgs e)
		{
			Offset = (int)numericUpDownOffset.Value;
			DrawSample(ShadowColor, Offset);
		}

		/// <summary>
		/// Draws the text "Sample" in the selected shadow brush settings.
		/// </summary>
		/// <param name="color"></param>
		/// <param name="offset"></param>
		private void DrawSample(Color color, int offset)
		{
			pictureBoxSample.Image = new Bitmap(pictureBoxSample.Size.Width, pictureBoxSample.Size.Height);
			using (Graphics lGraphics = Graphics.FromImage(pictureBoxSample.Image)) 
			{
				Font lSampleFont = new Font(FontFamily.GenericSansSerif, pictureBoxSample.Height - offset, FontStyle.Regular, GraphicsUnit.Pixel);
				lGraphics.DrawString("Sample", lSampleFont, new SolidBrush(color), 0, 0);
				lGraphics.DrawString("Sample", lSampleFont, new SolidBrush(SystemColors.WindowText), offset, offset);
				pictureBoxSample.Invalidate();
			}
		}
	}
}